package mil.af.amc.adpetracker.model;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Map;
import java.util.HashMap;

/**
 * Updated class to represent the model of a particular item.
 * <p/>
 * This particular class removes some of the "cruft" from EquipmentItem by
 * grouping items that are the same for a particular model with the model
 * instead of with the EquipmentItem
 *
 * @author TSgt Jason Ferguson
 */

@Entity
@Cache(usage= CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "T_MODEL")
public class ItemModel implements Serializable, Comparable<ItemModel>, MappableEntity {

    private int modelId;
    private String name;
    private Manufacturer manufacturer;
    private EquipmentType type;
    private int processorSpeed;
    private int screenSize;

    @Id
    @Column(name = "MODELID")
    public int getModelId() {
        return modelId;
    }

    public void setModelId(int modelId) {
        this.modelId = modelId;
    }

    @Transient
    public int getId() {
        return modelId;
    }

    @ManyToOne
    @JoinColumn(name = "MANUFACTURERID")
    public Manufacturer getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(Manufacturer manufacturer) {
        this.manufacturer = manufacturer;
    }

    @ManyToOne
    @JoinColumn(name = "TYPEID")
    public EquipmentType getType() {
        return type;
    }

    public void setType(EquipmentType type) {
        this.type = type;
    }

    @Column(name = "NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "PROCESSORSPEED")
    public int getProcessorSpeed() {
        return processorSpeed;
    }

    public void setProcessorSpeed(int processorSpeed) {
        this.processorSpeed = processorSpeed;
    }

    @Column(name = "SCREENSIZE")
    public int getScreenSize() {
        return screenSize;
    }

    public void setScreenSize(int screenSize) {
        this.screenSize = screenSize;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final ItemModel itemModel = (ItemModel) o;

        if (modelId != itemModel.modelId) return false;

        return true;
    }

    public int hashCode() {
        return modelId;
    }

    public int compareTo(ItemModel o) {

        return this.getName().toUpperCase().trim().compareTo(o.getName().toUpperCase().trim());
        //return new Integer(this.getModelId()).compareTo(new Integer(o.getModelId()));
    }

    public Map toMap() {

        Map map = new HashMap();
        map.put("id", this.getModelId());
        map.put("name", this.getName());

        return map;

    }
}